package com.java1234.jxc.controller.admin;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.java1234.jxc.beans.Goods;
import com.java1234.jxc.beans.GoodsType;
import com.java1234.jxc.service.GoodsTypeService;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin/goodsType")
public class GoodsTypeController {

    @Autowired
    GoodsTypeService goodsTypeService;

    @Autowired
    Dao dao;

    @RequestMapping("/save")
    public Object save(String name,Integer parentId){
        NutMap result = NutMap.NEW();
        GoodsType goodsType = new GoodsType();
        goodsType.setName(name);
        goodsType.setpId(parentId);
        goodsType.setIcon("icon-folder");
        goodsType.setState(0);

        GoodsType parent = goodsTypeService.fetch(parentId);
        if (parent!=null){
            parent.setState(1);
            goodsTypeService.updateIgnoreNull(parent);
        }

        goodsTypeService.insert(goodsType);
        result.put("success", true);
        return result;
    }

    @RequestMapping("/delete")
    public Object delete(Integer id){
        NutMap result = NutMap.NEW();
        GoodsType goodsType = goodsTypeService.fetch(id);
        List<Goods> goodsList = dao.query(Goods.class, Cnd.where("typeId", "=", id));

        // 如果该分类下有子分类不允许删除
        if (goodsTypeService.count(Cnd.where("pId","=",id))>0){
            result.put("success", false);
            result.put("errorInfo", "该类别下含有子分类，不能删除！");
            return result;
        }


        if (goodsList.size()==0){
            Integer pid = goodsType.getpId();
            List<GoodsType> goodsTypesByParent = goodsTypeService.query(Cnd.where("pId", "=", pid));
            if (goodsTypesByParent.size()==1){
                GoodsType parent = goodsTypeService.fetch(pid);
                parent.setState(0);
                goodsTypeService.update(parent);

            }
            goodsTypeService.delete(id);
            result.put("success", true);

        }else{
            result.put("success", false);
            result.put("errorInfo", "该类别下含有商品，不能删除！");
        }
        return result;
    }


    @RequestMapping("/loadTreeInfo")
    public String loadTreeInfo()throws Exception{
        return getAllByParentId(-1).toString();
    }

    /**
     * 根据父节点id获取所有类别集合
     * @param parentId
     * @return
     */

    public JsonArray getAllByParentId(Integer parentId){
        JsonArray jsonArray = this.getParentId(parentId);
        for(int i=0;i<jsonArray.size();i++){
            JsonObject jsonObject = (JsonObject) jsonArray.get(i);

            if ("open".equals(jsonObject.get("state").getAsString())){
                continue;
            }else{
                jsonObject.add("children",getAllByParentId(jsonObject.get("id").getAsInt()));
            }

        }
        return jsonArray;
    }


    /**
     * 根据父节点查询所有子节点
     * @param parentId
     * @return
     */
    public JsonArray getParentId(Integer parentId){
        List<GoodsType> goodsTypeList = goodsTypeService.query(Cnd.where("pId", "=", parentId));

        JsonArray jsonArray = new JsonArray();

        goodsTypeList.forEach(goodsType -> {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("id",goodsType.getId());
            jsonObject.addProperty("text",goodsType.getName());
            if(goodsType.getState()==1){
                jsonObject.addProperty("state", "closed"); // 根节点
            }else{
                jsonObject.addProperty("state", "open"); // 叶子节点
            }
            jsonObject.addProperty("iconCls", goodsType.getIcon()); // 节点图标
            JsonObject attributeObject=new JsonObject(); // 扩展属性
            attributeObject.addProperty("state", goodsType.getState()); // 节点状态
            jsonObject.add("attributes", attributeObject);
            jsonArray.add(jsonObject);
        });

        return jsonArray;
    }


}
